var utils = {
    getIndex: function (el) {
        return [].indexOf.call(el.parentNode.children, el);
    },
    observer: {},
    listen: function (key, func) {
        if(this.observer.hasOwnProperty(key)){
            this.observer[key].push(func);
        }else{
            this.observer[key] = [func];
        }
    },
    trigger: function (key, args) {
        if(this.observer.hasOwnProperty(key)){
            var i = 0,
                len = this.observer[key].length;
            for( ; i < len; i++){
                this.observer[key][i].apply(null, args);
            }
        }
    },
    buildParams: function (data) {
        var i,
            params = '';
        for(var i in data){
            if(data.hasOwnProperty(i)){
                params += '&' + i + '=' + data[i];
            }
        }

        return params.slice(1);
    },
    creatElement: function (tag, option, content) {
        var el = document.createElement(tag),
            i;
        for ( i in option) {
            if(option.hasOwnProperty(i)){
                el.setAttribute(i, option[i]);
            }
        }

        if(content){
            el.innerHTML = content;
        }
        return el;
    },
    getParameterByName: function(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    },
    bubblingToEle: function (target, type, value) {
        while(target){

            switch (type){
                case 'id':
                    if(target.id === value){
                        return target;
                    }
                    break;

                case 'nodeName':
                    if(target.nodeName === value){
                        return target;
                    }
                    break;

                case 'className':
                    if(target.classList.contains(value)){
                        return target;
                    }
                    break;

                default :
                    break;
            }

            target = target.parentNode;

            if(target.nodeName === 'HTML'){
                break;
            }
        }
        return null;
    }
}

export default utils